package com.tang.subject.domain.handler.subject;

import com.tang.subject.common.enums.SubjectTypeEnum;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SubjectTypeHandlerFactory implements InitializingBean {

    @Resource
    private List<SubjectTypeHandler> handlerList;

    private Map<SubjectTypeEnum, SubjectTypeHandler> handlerMap = new HashMap<>();

    /**
     * 根据题目类型选取相应的策略对象
     *
     * @param subType
     * @return com.tang.subject.domain.handler.subject.SubjectTypeHandler
     * @author Tang
     * @date 2024/2/4 14:33
     **/
    public SubjectTypeHandler getHandler(int subType) {
        return handlerMap.get(SubjectTypeEnum.getByCode(subType));
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        for (SubjectTypeHandler subjectTypeHandler : handlerList) {
            handlerMap.put(subjectTypeHandler.getSubjectType(), subjectTypeHandler);
        }
    }
}
